package com.fone.player.play.adapter;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.fone.player.ApplicationManage;
import com.fone.player.R;
import com.fone.player.client.VgdetailRst.Cnt;
import com.fone.player.entity.ContentSubscribe;
import com.fone.player.storage.StorageModule;
import com.fone.player.util.FoneUtil;

/**
 * 节目单adapter
 * 
 * @author kongxiaojun
 * @since 2014-4-30
 */
public class VideoLiveCardAdapter extends BaseAdapter {

	/** 节目单list */
	private List<ContentSubscribe> subscribes;
	/** 正在播放的位置 */
	private int playingPostion;

	public VideoLiveCardAdapter(List<ContentSubscribe> subscribes) {
		super();
		this.subscribes = subscribes;
		updatePlayingPostion();
	}

	@Override
	public int getCount() {
		if (subscribes == null) {
			return 0;
		}
		return subscribes.size();
	}

	@Override
	public Object getItem(int position) {
		if (subscribes == null || subscribes.size() == 0) {
			return null;
		} else {
			return subscribes.get(position);
		}
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// if (position == 3) {
		// convertView =
		// LayoutInflater.from(mContext).inflate(R.layout.video_livecard_item_day,
		// null);
		// } else {
		ContentSubscribe sub = subscribes.get(position);
		if (sub.getContentSubscribeTime() == 0) {
			convertView = LayoutInflater.from(ApplicationManage.getAplicationManageInstance()).inflate(R.layout.video_livecard_item_day, null);
			TextView day = (TextView) convertView.findViewById(R.id.video_livecard_date);
			day.setText(sub.getContentSubscribeName());
		} else {
			convertView = LayoutInflater.from(ApplicationManage.getAplicationManageInstance()).inflate(R.layout.video_livecard_item_normal, null);
			ContentSubscribe sub2 = StorageModule.getInstance().getContentSubscribeByNameAndTime(sub.getContentSubscribeName(), sub.getContentSubscribeTime());
			TextView tvReservation = (TextView) convertView.findViewById(R.id.video_livecard_reservation);
			TextView tvDate = (TextView) convertView.findViewById(R.id.video_livecard_date);
			TextView tvName = (TextView) convertView.findViewById(R.id.video_livecard_name);
			// 是否正在播放
			if (subscribes.size() > position + 1 && sub.getContentSubscribeTime() < System.currentTimeMillis()) {
				//
				ContentSubscribe next = subscribes.get(position + 1);
				if (next.getContentSubscribeTime() == 0 && subscribes.size() > position + 2) {
					next = subscribes.get(position + 2);
				}
				if (System.currentTimeMillis() < next.getContentSubscribeTime()) {
					// 是当前播放的
					this.playingPostion = position;
					tvDate.setTextColor(ApplicationManage.getAplicationManageInstance().getResources().getColor(R.color.video_info_tab_line));
					tvName.setTextColor(ApplicationManage.getAplicationManageInstance().getResources().getColor(R.color.video_info_tab_line));
				}
			}
			if (sub2 != null) {
				tvReservation.setText(R.string.has_reservation);
				tvReservation.setTextColor(ApplicationManage.getAplicationManageInstance().getResources().getColor(R.color.line_video_reservation));
			} else {
				tvReservation.setText(R.string.reservation);
				tvReservation.setTextColor(ApplicationManage.getAplicationManageInstance().getResources().getColor(R.color.video_info_tab_line));
			}

			if (sub.getContentSubscribeTime() < System.currentTimeMillis()) {
				tvReservation.setVisibility(View.GONE);
			} else {
				tvReservation.setVisibility(View.VISIBLE);
			}
			tvDate.setText(FoneUtil.formatDate(new Date(sub.getContentSubscribeTime()), "MM-dd HH:mm"));
			tvName.setText(sub.getContentSubscribeName());
		}
		return convertView;
	}

	/**
	 * 更新当前播放位置
	 * 
	 * @return void
	 * @throws
	 */
	private void updatePlayingPostion() {
		if (subscribes != null && subscribes.size() > 0) {
			for (int i = 0; i < subscribes.size(); i++) {
				ContentSubscribe sub = subscribes.get(i);
				// 是否正在播放
				if (subscribes.size() > i + 1 && sub.getContentSubscribeTime() < System.currentTimeMillis()) {
					//
					ContentSubscribe next = subscribes.get(i + 1);
					if (next.getContentSubscribeTime() == 0 && subscribes.size() > i + 2) {
						next = subscribes.get(i + 2);
					}
					if (System.currentTimeMillis() < next.getContentSubscribeTime()) {
						// 是当前播放的
						this.playingPostion = i;
						break;
					}
				}
			}
		}
	}

	public int getPlayingPostion() {
		return playingPostion;
	}
}
